
let id = 0;
class Dep{
   constructor(){
     this.id = id++;
     this.subs = [];
   }
   addSub(){
     this.subs.push(Dep.target); //观察者模式
   }
   depend(){
     Dep.target.addDep(this);
    //  this.subs.push(Dep.target); //观察者模式
   }
   notify(){
     this.subs.forEach(watcher=> watcher.update());
   }
   removeSub(sub){
        const index = this.subs.indexOf(sub);
        if(index>-1){
            return this.subs.splice(index,1);
        }
   }
}
let stack = [];
export function pushTarget(watcher){
    Dep.target = watcher;
    stack.push(watcher);
}
export function popTarget(){
   stack.pop();
   Dep.target = stack[stack.length - 1];
}
export default Dep;
